package com.zhy.sunmi.utils;

import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.sunmi.MainActivity2;
import com.zhy.sunmi.MyApplication;
import com.zhy.sunmi.TestPrintInfo;

import java.io.UnsupportedEncodingException;

import POSAPI.POSInterfaceAPI;
import POSSDK.POSSDK;

import static POSSDK.POSSDK.PRINT_MODE_PAGE;
import static com.zhy.sunmi.MyApplication.POS_SUCCESS;
import static com.zhy.sunmi.MyApplication.pos_usb;
import static com.zhy.sunmi.MyApplication.testprint;

public class PrintUtil {

//    public static void printText(String str, int alignment, int fontStyle){
//        int length=0;
//        try {
//            length = str.getBytes("GBK").length;
//        } catch (UnsupportedEncodingException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//
//        int error_code = testprint.TestPrintText(pos_usb, 1, str, length, 0, fontStyle,
//                alignment, 10, 20, 10, 1, 1);
//        if (error_code != POS_SUCCESS) {
//            Toast.makeText(MyApplication.getInstance().getApplicationContext(), "Failed to print Text.", Toast.LENGTH_LONG).show();
//        }
//
//    }

    public static void printText(String str, int alignment, int fontStyle) {
        printText(str,alignment,fontStyle,10);
    }

    public static void printText(int a,String str, int alignment, int fontStyle) {
        printText(a,str,alignment,fontStyle,10);
    }
    public static void printText(String str, int alignment, int fontStyle,int lineHeight) {
        int f = 0;
        switch (fontStyle) {
            case 1:
                f |= 0x08;
                break;
            case 2:
                f |= 0x80;
                break;
            case 3:
                f |= 0x08;
                f |= 0x80;
                break;
            default:
                f = 0;
        }
        int error_code = testprint.TestPrintText(pos_usb, 0, str, 0, 0, f,
                alignment, 0, 20, lineHeight, 1, 1);
        if (error_code != POS_SUCCESS) {
            Toast.makeText(MyApplication.getInstance().getApplicationContext(), "Failed to print Text.", Toast.LENGTH_LONG).show();
        }

    }

    public static void printText(int times,String str, int alignment, int fontStyle,int lineHeight) {
        int f = 0;
        switch (fontStyle) {
            case 1:
                f |= 0x08;
                break;
            case 2:
                f |= 0x80;
                break;
            case 3:
                f |= 0x08;
                f |= 0x80;
                break;
            default:
                f = 0;
        }
        int error_code = testprint.TestPrintText(pos_usb, 0, str, 0, 0, f,
                alignment, 0, 20, lineHeight, 1, times);
        if (error_code != POS_SUCCESS) {
            Toast.makeText(MyApplication.getInstance().getApplicationContext(), "Failed to print Text.", Toast.LENGTH_LONG).show();
        }

    }

    public static void cutPaper(){
        testprint.TestCutPaper(pos_usb, 0);
    }





    public static void printBar(String str){
//        if(pos_usb==null){
//            Toast.makeText(MyApplication.getInstance().getApplicationContext(), "Please connect the printer", Toast.LENGTH_LONG).show();
//            return;
//        }
        int length=0;
        try {
            length = str.getBytes("GB18030").length;
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int error_code = testprint.TestPrintBar(pos_usb, 0, str, length,
                73, 2, 80, 1, 0);
        if(error_code != POS_SUCCESS)
        {
            Toast.makeText(MyApplication.getInstance().getApplicationContext(), "Failed to print Barcode.",Toast.LENGTH_LONG).show();
        }
    }
}
